package org.example.atry.pqdao;

import org.example.atry.dbutil.nbutil;
import org.example.atry.util.ousers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class cdao {
    public boolean queryname(String username) {
        ousers obj = new ousers();
        Connection cn = nbutil.getCon();
        try {
            String select = "select * from logind where uername = ?";
            //统计数量：String select="select count(*)as num from where username like ?";
            PreparedStatement ps = cn.prepareStatement(select);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {

                  return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            nbutil.closeConn();
        }
        return false;
    }
    public boolean querypassword(String username, String password) {
        ousers obj = new ousers();
        Connection cn = nbutil.getCon();
        try {
            String select = "select * from logind where uername = ? and password =?; ";
            PreparedStatement ps = cn.prepareStatement(select);

            ps.setString(1,username);
            ps.setString(2,password);
//            ResultSet 的游标初始位置是在第一行之前，rs.next() 的作用是将游标向下移动一行，并返回一个布尔值表示是否成功移动。
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            nbutil.closeConn();
        }
        return false;
    }
    public Integer zen(String username, String password) {
        Connection cn = nbutil.getCon();
        try {
            String  insert = "insert into logind (uername,password) values (?,?)";
            PreparedStatement ps =cn.prepareStatement(insert);
            ps.setString(1,username);
            ps.setString(2,password);
            int result = ps.executeUpdate();
            System.out.println(result);
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            nbutil.closeConn();
        }
    }
    public Integer delete(String username,String password) {
        Connection cn = nbutil.getCon();
        try {
            String  insert = "delete from logind where uername= ? and password= ?";
            PreparedStatement ps =cn.prepareStatement(insert);
            //创建执行增删改查的工具，以及为语句中的 ? 注入数据
            ps.setString(1,username);
            ps.setString(2,password);
            int result = ps.executeUpdate();
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            nbutil.closeConn();
        }

    }
    public Integer shift(String username, String password) {
        Connection cn = nbutil.getCon();
        try {
            String  insert = "update logind set password=? where uername=?";
            PreparedStatement ps =cn.prepareStatement(insert);
            ps.setString(1,password);
            ps.setString(2,username);
            int result = ps.executeUpdate();
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            nbutil.closeConn();
        }
    }
}
